package com.code.zg.news.utils.manager;

import android.app.Activity;

import java.util.Stack;

/**
 * Created by zhanggang on 2017/2/22.
 */

public class ActivityManager {
    private static ActivityManager activityManager;
    private static Stack<Activity> stack;
    private ActivityManager(){
        stack=new Stack<>();
    }
    public static ActivityManager getInstance(){
        if(activityManager==null){
            synchronized (ActivityManager.class){
                if(activityManager==null){
                    activityManager=new ActivityManager();
                }
            }
        }
        return activityManager;
    }
    public void addActivity(Activity activity){
        if(activity!=null){
            stack.add(activity);
        }
    }
    public void removeActivity(Activity activity){
        if(activity!=null){
            activity.finish();
            stack.remove(activity);
        }
    }
    public void finishCurrentActivity(){
        if(!stack.isEmpty()){
            stack.lastElement().finish();
            stack.remove(stack.lastElement());

        }
    }
    public void finishAll(){
        for(Activity activity:stack){
            activity.finish();
        }
        stack.clear();
    }
    public void finishActiivty(Class<?>clazz){
        if(!stack.isEmpty()){
            for (Activity activity:stack){
                if (activity.getClass().equals(clazz)) {
                    activity.finish();
                    stack.remove(activity);
                }
            }
        }
    }
}
